package com.kkd.kkdapp.activity;

import java.text.DecimalFormat;

import org.json.JSONException;
import org.json.JSONObject;

import com.kkd.kkdapp.R;
import com.kkd.kkdapp.base.BaseActivity;
import com.kkd.kkdapp.http.manager.HttpCallBack;
import com.kkd.kkdapp.http.manager.KkdHttpMagager;
import com.kkd.kkdapp.utils.AppUtil;
import com.kkd.kkdapp.utils.DialogUtils;
import com.kkd.kkdapp.utils.LogUtil;
import com.kkd.kkdapp.utils.PreferenceUtils;
import com.kkd.kkdapp.utils.SystemUtils;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

/******
 * 
 * @author mty
 * 
 * @Name 账户余额
 * 
 */
public class BalanceActivity extends BaseActivity implements OnClickListener {

	private final int REQUEST_SUCCESS = 0; // 请求成功
	private RelativeLayout top_img; // 返回键
	private TextView balance_tv; // 账户余额 TextView
	private double balance = 0.00; // 账户余额
	private TextView profit_tv; // 存钱罐收益 TextView
	private String balanceIncome; // 存钱罐收益
	private Button recharge_bt; // 充值
	private Button withdrawals_bt; // 提现
	private TextView describe_tv; // 描述 TextView
	private String savingPotDesc; // // 描述
	private TextView what_tv; // 什么是存钱罐
	private DecimalFormat df = new DecimalFormat("0.01");
	private DecimalFormat df1 = new DecimalFormat("###,##0.01");
	private Dialog mDialog;

	private Handler mHandler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case REQUEST_SUCCESS:
				updateUI();
				break;
			}

		};
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_balance);
		initView();
		setListener();
	}

	/************
	 * 初始化控件
	 ************/
	public void initView() {
		top_img = (RelativeLayout) this.findViewById(R.id.top_img);
		balance_tv = (TextView) this.findViewById(R.id.balance_tv);
		profit_tv = (TextView) this.findViewById(R.id.profit_tv);
		recharge_bt = (Button) this.findViewById(R.id.recharge_bt);
		withdrawals_bt = (Button) this.findViewById(R.id.withdrawals_bt);
		describe_tv = (TextView) this.findViewById(R.id.describe_tv);
		what_tv = (TextView) this.findViewById(R.id.what_tv);

	}

	/***********
	 * 注册监听事件
	 ***********/
	public void setListener() {
		top_img.setOnClickListener(this);
		recharge_bt.setOnClickListener(this);
		withdrawals_bt.setOnClickListener(this);
		what_tv.setOnClickListener(this);
	}

	Intent intent;

	@Override
	public void onClick(View v) {

		switch (v.getId()) {
		case R.id.top_img: // 返回键
			finish();
			break;
		case R.id.recharge_bt: // 充值
			SystemUtils.onEvent("Account_Blance_Recharge", 1);
			if (AppUtil.isConnect(this)) {
				if (PreferenceUtils.getInstance().getAutonym(this) == 1
						&& PreferenceUtils.getInstance().getBoundBankCard(this) == 1) {
					intent = new Intent(this, RechargeActivity.class);
					startActivity(intent);
				} else {
					intent = new Intent(this, ForTenderActivity.class);
					intent.putExtra("enterTypeFlag", 3);
					startActivity(intent);
				}
			} else {
				showToast(R.string.network_anomaly);
			}
			break;
		case R.id.withdrawals_bt: // 提现
			SystemUtils.onEvent("Account_Blance_WithDraw", 1);
			if (PreferenceUtils.getInstance().getAutonym(this) == 1) {
				if (PreferenceUtils.getInstance().getBoundBankCard(this) == 1) {
					if (PreferenceUtils.getInstance().getWithdrawPassWord(this) == 1) {
						intent = new Intent(this, WithdrawActivity.class);
						startActivity(intent);
					} else if (PreferenceUtils.getInstance()
							.getWithdrawPassWord(this) == -1) {
						requestIsSetPayPassword();

					} else {
						requestIsSetPayPassword();
					}

				} else {
					intent = new Intent(this, ForTenderActivity.class);
					intent.putExtra("addbank", 2);
					intent.putExtra("enterTypeFlag", 2);
					startActivity(intent);
				}
			} else {
				intent = new Intent(this, ForTenderActivity.class);
				intent.putExtra("trueidflag", 2);
				intent.putExtra("enterTypeFlag", 2);
				startActivity(intent);
			}
			break;
		case R.id.what_tv: // 什么是存钱罐
			intent = new Intent(this, WhatIsAPiggyBankActivity.class);
			startActivity(intent);
			break;
		}

	}

	@Override
	protected void onResume() {
		super.onResume();
		if (PreferenceUtils.getInstance().getLogin(this) == 0) {
			requestBalanceInterface();
		}
	}

	/***********
	 * 请求余额接口
	 ***********/
	public void requestBalanceInterface() {
		KkdHttpMagager.getInstance().getAccountInfoManager(  String.class,
				new HttpCallBack() {

					@Override
					public void onSuccess(int statusCode, Object responseBody) {
						try {
							String responseString = responseBody.toString();
							JSONObject data = new JSONObject(responseString);
							balance = data.getDouble("balance");
							balanceIncome = data.getString("balanceIncome");
							savingPotDesc = data.getString("savingPotDesc");
							mHandler.sendEmptyMessage(REQUEST_SUCCESS);

						} catch (JSONException e) {
							e.printStackTrace();
						}
					}

					@Override
					public void onFailure(int statusCode, String desc) {
						showToast(desc);
					}
				});
	}

	/**********
	 * 更新UI
	 **********/
	public void updateUI() {
		balance_tv.setText(df1.format(balance));
		profit_tv.setText(df.format(Double.parseDouble(balanceIncome)));
		describe_tv.setText(savingPotDesc);
	}

	/**
	 * 
	 * 判断是否设置交易密码
	 */
	public void requestIsSetPayPassword() {
		KkdHttpMagager.getInstance().getWithdrawUpdateManager( String.class,
				new HttpCallBack() {

					@Override
					public void onSuccess(int statusCode, Object responseBody) {
						try {
							String responseString = responseBody.toString();
							JSONObject data = new JSONObject(responseString);
							LogUtil.i("测试", "1");
							int isPayPassword = data.getInt("isPayPassword");
							if (isPayPassword == 1) {
								PreferenceUtils.getInstance()
										.setWithdrawPassWord(
												BalanceActivity.this, 1);
							} else {
								PreferenceUtils.getInstance()
										.setWithdrawPassWord(
												BalanceActivity.this, 0);
								showDialog();
							}
						} catch (JSONException e) {

							e.printStackTrace();
						}
						AppUtil.stopProgressDialog();
					}

					@Override
					public void onFailure(int statusCode, String desc) {
						AppUtil.stopProgressDialog();
						showToast(BalanceActivity.this, desc);
					}
				});
	}

	public void showToast(Activity acitivity, String hintLanguage) {
		AppUtil.makeToastForCustomer(acitivity, hintLanguage);
	}

	public void showDialog() {
		if (mDialog != null) {
			mDialog.cancel();
			mDialog = null;
		}
		mDialog = DialogUtils.deleteBankDialog(this, "您尚未设置提现密码，请先去设置提现密码",
				new OnClickListener() {

					@Override
					public void onClick(View v) {
						switch (v.getId()) {
						case R.id.confirm_deletebank:
							Intent intent = new Intent(BalanceActivity.this,
									SetDealActivity.class);
							intent.putExtra("setFlag", 0);
							startActivity(intent);
							if (mDialog != null) {
								mDialog.dismiss();
							}
							break;
						case R.id.cancel_deletebank:
							if (mDialog != null) {
								mDialog.dismiss();
							}
							break;
						}
					}
				});
		mDialog.setCancelable(false);
		mDialog.show();
	}

}
